package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.Menus;
import com.example.mapper.MenusMapper;
import com.example.service.IMenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengc
 * @since 2023-05-12
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements IMenusService {

    @Override
    public List<Menus> findMenus() {
        //查询所有数据
        List<Menus> list = list();
        //找出pid为null的一级菜单
        List<Menus> parentNodes = list.stream().filter(menu -> menu.getParentid() == null).collect(Collectors.toList());
        for (Menus menu:parentNodes) {
            //找到一级菜单的子菜单
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getParentid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
